#ifndef __INTERLOCK_H__
#define __INTERLOCK_H__

/**
*@file       interlock.h
*@brief      global function impl inter lock operation
*
*@author     graywind
*@version    1.0.0.0
*@data       2011/11/20
*/

#ifdef WIN32
#include <intrin.h>

class inter_lock
{
public:
	///@brief     impl atomic operate to exchange this val
	static long exchange(long* pTarget, long val)
	{
		return InterlockedCompareExchange(pTarget,val,*pTarget);
	}

    ///@brief     impl atomic operate to add this val
    static long add(long* pTarget, long val)
    {
        return InterlockedExchangeAdd(pTarget, val);
    }

    ///@brief     impl atomic operate to sub this val
    static long sub(long* pTarget, long val)
    {
        return InterlockedExchangeAdd(pTarget, -val);
    }
};



#else

class inter_lock
{
public:
	///@brief     impl atomic operate to exchange thes val
	static long exchange(long* pTarget, long val)
	{
		return __sync_val_compare_and_swap(pTarget, *pTarget, val);
	}

    ///@brief     impl atomic operate to add this val
    static long add(long* pTarget, long val)
    {
        return __sync_fetch_and_add(pTarget, val);
    }

    ///@brief     impl atomic operate to sub this val
    static long sub(long* pTarget, long val)
    {
        return __sync_fetch_and_sub(pTarget, val);
    }
};

#endif


#endif
